﻿package mortal.game.view.guild.otherpanel
{
    import Message.Public.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import extend.language.*;
    import fl.data.*;
    import flash.events.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.game.cache.*;
    import mortal.game.cache.guild.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.pageSelect.*;
    import mortal.game.view.guide.*;
    import mortal.game.view.guild.cellrender.*;
    import mortal.game.view.guildNew.data.*;
    import mortal.game.view.systemSetting.*;
    import mortal.game.view.vip.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class GuildListPanel extends GuildOtherBasePanel
    {
        private var _comboBoxFilterCamp:GComboBox;
        private var _checkBoxFilterFullMemberGuild:GCheckBox;
        private var _txtSearch:GTextInput;
        private var _btnSearch:GButton;
        private var _btnReset:GButton;
        private var _pageSelecter:PageSelecter;
        private var _txtMyGuild:GTextFiled;
        private var _txtMyGuildRank:GTextFiled;
        private var _btnCreateGuild:GButton;
        private var _btnInviteList:GButton;
        private var _guildList:GTileList;
        private var _arrow:GuideArrow;
        private var _circle:GSprite;
        private var _NullGuildTxt:GTextFiled;
        private var _freeGuildNumTxt:GTextFiled;
        public static var FreeCreateGuildVipLv:int = 3;
        public static var PanelType:int = 0;
        private static var _instance:GuildListPanel = null;
        private static const PAGE_CELL_COUNT:int = 10;

        public function GuildListPanel(param1:ILayer = null)
        {
            super(param1);
            setSize(690, 485);
            title = "" + Language.getString(71185);
            return;
        }// end function

        override protected function addEvents() : void
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            UIFactory.bg(16, 67, 668, 358, this);
            UIFactory.bg(16, 67, 668, 26, this, ImagesConst.RegionTitleBg);
            var _loc_1:Array = [{label:Language.getString(60053), type:0}, {label:GameDefConfig.instance.getItem("ECamp", 1).text, type:1}, {label:GameDefConfig.instance.getItem("ECamp", 2).text, type:2}, {label:GameDefConfig.instance.getItem("ECamp", 3).text, type:3}];
            var _loc_2:* = new DataProvider(_loc_1);
            this._comboBoxFilterCamp = UIFactory.gComboBox(64, 40, 80, 22, _loc_2, this);
            this._comboBoxFilterCamp.configEventListener(Event.CHANGE, this.onCampSelectChange);
            this._comboBoxFilterCamp.visible = false;
            this._checkBoxFilterFullMemberGuild = UIFactory.checkBox("" + Language.getString(71186), 24, 42, 120, 24, this);
            UIFactory.gTextField(Language.getString(60046) + ":", 354, 42, 65, 20, this, GlobalStyle.textFormatJiang);
            this._txtSearch = UIFactory.gTextInput(415, 42, 144, 20, this);
            this._btnSearch = UIFactory.gButton(Language.getString(60050), 564, 40, 54, 24, this);
            this._btnReset = UIFactory.gButton(Language.getString(60051), 623, 40, 54, 24, this);
            var _loc_3:* = GlobalStyle.textFormatBai;
            _loc_3.align = "center";
            this._pageSelecter = UIFactory.pageSelecter(290, 400, this, PageSelecter.CompleteMode);
            this._pageSelecter.setbgStlye(ImagesConst.ComboBg, _loc_3);
            this._pageSelecter.maxPage = 3;
            this._pageSelecter.currentPage = 1;
            this._pageSelecter.pageTextBoxSize = 45;
            this._pageSelecter.configEventListener(Event.CHANGE, this.onPageChange);
            var _loc_4:* = "<a href=\'event:myGuildLink\' target = \'\'><font color=\'#00ff00\'><u>" + Language.getString(60049) + "</u></font></a>";
            this._txtMyGuild = UIFactory.gTextField(_loc_4, 546, 400, 56, 20, this, null, true);
            this._txtMyGuild.configEventListener(TextEvent.LINK, this.onTextLink);
            var _loc_5:* = "<font color=\'#ffffff\'>" + Language.getString(60052) + " : </font><font color=\'#ffcc00\'>108</font>";
            this._txtMyGuildRank = UIFactory.gTextField(_loc_5, 615, 400, 60, 20, this, null, true);
            UIFactory.gTextField(Language.getString(60052), 24, 71, 30, 20, this);
            UIFactory.gTextField(Language.getString(60046), 88, 71, 65, 20, this);
            UIFactory.gTextField(Language.getString(60058), 200, 71, 30, 20, this);
            UIFactory.gTextField(Language.getString(60087), 290, 71, 65, 20, this);
            UIFactory.gTextField(Language.getString(60060), 393, 71, 65, 20, this);
            UIFactory.gTextField(Language.getString(60059), 485, 71, 65, 20, this);
            UIFactory.gTextField(Language.getString(60033), 588, 71, 30, 30, this);
            this._btnCreateGuild = UIFactory.gButton(Language.getString(60047), 272, 453, 64, 24, this);
            this._btnInviteList = UIFactory.gButton(Language.getString(60048), 362, 453, 64, 24, this);
            this._guildList = UIFactory.tileList(16, 94, 668, 305, this);
            this._guildList.rowHeight = 30;
            this._guildList.columnWidth = 668;
            this._guildList.horizontalGap = 1;
            this._guildList.verticalGap = 0.5;
            this._guildList.setStyle("cellRenderer", GuildListCellRenderer);
            this._NullGuildTxt = UIFactory.gTextField("", 70, 200, 600, 40, this, GlobalStyle.textFormatItemGreen);
            this._NullGuildTxt.htmlText = "<font size=\'22\'>" + Language.getString(71187) + "</font>";
            this._NullGuildTxt.visible = false;
            this._freeGuildNumTxt = UIFactory.gTextField(Language.getStringByParam(70542, 2), 195, 160, 285, 60);
            this._freeGuildNumTxt.visible = false;
            this.getDataFromServer();
            NetDispatcher.addCmdListener(ServerCommand.GuildApplySuccess, this.updateGuide);
            super.addEvents();
            return;
        }// end function

        public function set filterFullMemberGuildVisible(param1:Boolean) : void
        {
            this._checkBoxFilterFullMemberGuild.visible = param1;
            return;
        }// end function

        private function getDataFromServer() : void
        {
            if (PanelType)
            {
                this._comboBoxFilterCamp.selectedIndex = 0;
            }
            else
            {
                this._comboBoxFilterCamp.selectedIndex = Cache.instance.role.roleEntityInfo.entityInfo.camp;
            }
            var _loc_1:* = new Object();
            _loc_1.camp = PanelType == 1 ? (0) : (Cache.instance.role.roleEntityInfo.entityInfo.camp);
            _loc_1.guildName = "";
            _loc_1.startIdx = 0;
            _loc_1.isfull = false;
            Dispatcher.dispatchEvent(new DataEvent(EventName.GUILD_SEARCH, _loc_1));
            return;
        }// end function

        private function sortGuild(param1:SMiniGuildInfo, param2:SMiniGuildInfo) : int
        {
            if (param1.guildType == 1 && param2.guildType != 1)
            {
                return -1;
            }
            if (param1.guildType != 1 && param2.guildType == 1)
            {
                return 1;
            }
            if (param1.combat > param2.combat)
            {
                return -1;
            }
            if (param1.combat < param2.combat)
            {
                return 1;
            }
            if (param1.level > param2.level)
            {
                return -1;
            }
            if (param1.level < param2.level)
            {
                return 1;
            }
            return 0;
        }// end function

        private function changeRank(param1:Array) : void
        {
            var _loc_3:SMiniGuildInfo = null;
            if (param1 == null)
            {
                return;
            }
            var _loc_2:int = 0;
            while (_loc_2 < param1.length)
            {
                
                _loc_3 = param1[_loc_2];
                _loc_3.rank = _loc_2 + 1;
                _loc_2++;
            }
            return;
        }// end function

        override public function update() : void
        {
            var _loc_6:SMiniGuildInfo = null;
            if (!isLoadComplete || isHide)
            {
                return;
            }
            if (!Cache.instance.guild.guildList)
            {
                return;
            }
            var _loc_1:* = Cache.instance.guild;
            this._pageSelecter.maxPage = _loc_1.guildTotalCount / PAGE_CELL_COUNT == 0 ? (1) : (Math.ceil(_loc_1.guildTotalCount / PAGE_CELL_COUNT));
            var _loc_2:* = new DataProvider();
            var _loc_3:* = _loc_1.guildList.length;
            var _loc_4:* = _loc_1.guildList.concat();
            _loc_4 = _loc_4.sort(this.sortGuild);
            this.changeRank(_loc_4);
            var _loc_5:int = 0;
            while (_loc_5 < _loc_3)
            {
                
                if (_loc_5 >= _loc_1.guildList.length)
                {
                    break;
                }
                _loc_6 = _loc_4[_loc_5];
                _loc_2.addItem(_loc_6);
                _loc_5++;
            }
            this._guildList.dataProvider = _loc_2;
            this._guildList.drawNow();
            if (_loc_3 > 0)
            {
                this._NullGuildTxt.visible = false;
            }
            else
            {
                this._NullGuildTxt.visible = true;
            }
            this.updateGuide();
            EffectManager.glowFilterUnReg(this._btnCreateGuild);
            if (!_loc_1.selfGuildInfo.selfHasJoinGuild)
            {
                var _loc_7:Boolean = false;
                this._txtMyGuildRank.visible = false;
                this._txtMyGuild.visible = _loc_7;
                if (_loc_1.freeCreateGuildNum < GuildNewUtil.freeGuildNum)
                {
                    this._freeGuildNumTxt.visible = true;
                    this._freeGuildNumTxt.htmlText = Language.getStringByParam(70542, GuildNewUtil.freeGuildNum - _loc_1.freeCreateGuildNum);
                    EffectManager.glowFilterReg(this._btnCreateGuild);
                }
                else
                {
                    this._freeGuildNumTxt.visible = false;
                }
            }
            else
            {
                this._freeGuildNumTxt.visible = false;
                var _loc_7:Boolean = true;
                this._txtMyGuildRank.visible = true;
                this._txtMyGuild.visible = _loc_7;
                this._txtMyGuildRank.htmlText = "<font color=\'#ffffff\'>" + Language.getString(60052) + " : </font><font color=\'#ffcc00\'>" + Cache.instance.guild.selfGuildInfo.baseInfo.rank + "</font>";
            }
            return;
        }// end function

        public function updateGuide(param1:Object = null) : void
        {
            var _loc_4:int = 0;
            var _loc_2:* = ClientSetting.local.getIsDone(IsDoneType.HasGuideGuild);
            var _loc_3:* = Cache.instance.guild.hasGuild;
            if (!_loc_3 && !_loc_2 && this._arrow == null)
            {
                if (this._guildList != null && this._guildList.dataProvider.length > 0)
                {
                    _loc_4 = this._guildList.dataProvider.length * 30 + 8;
                    this._circle = UICompomentPool.getUICompoment(GSprite) as GSprite;
                    this._circle.graphics.clear();
                    this._circle.graphics.lineStyle(2, 65280, 1, true);
                    this._circle.graphics.drawRoundRect(0, 0, 90, _loc_4, 24, 24);
                    this._circle.x = this._guildList.x + this._guildList.width - 127;
                    this._circle.y = this._guildList.y - 4;
                    this.addChild(this._circle);
                    this._arrow = UICompomentPool.getUICompoment(GuideArrow) as GuideArrow;
                    this._arrow.direction = GuideArrow.Left;
                    this._arrow.label = "" + Language.getString(71188);
                    this._arrow.x = this._circle.x + 88;
                    this._arrow.y = this._circle.y + this._circle.height / 2;
                    this.addChild(this._arrow);
                }
            }
            else if (_loc_2)
            {
                if (this._arrow != null)
                {
                    this._arrow.dispose(true);
                    this._arrow = null;
                }
                if (this._circle != null)
                {
                    this._circle.dispose(true);
                    this._circle = null;
                }
            }
            return;
        }// end function

        override public function onMouseClick(event:MouseEvent) : void
        {
            var _loc_2:Object = null;
            switch(event.target)
            {
                case this._btnSearch:
                {
                    _loc_2 = new Object();
                    _loc_2.camp = this._comboBoxFilterCamp.selectedItem == null ? (0) : (this._comboBoxFilterCamp.selectedItem.type);
                    _loc_2.guildName = this._txtSearch.text;
                    _loc_2.startIdx = 0;
                    _loc_2.isfull = this._checkBoxFilterFullMemberGuild.selected;
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GUILD_SEARCH, _loc_2));
                    break;
                }
                case this._btnReset:
                {
                    this._txtSearch.text = "";
                    this._comboBoxFilterCamp.selectedIndex = Cache.instance.role.roleEntityInfo.entityInfo.camp;
                    _loc_2 = new Object();
                    _loc_2.camp = Cache.instance.role.roleEntityInfo.entityInfo.camp;
                    _loc_2.guildName = "";
                    _loc_2.startIdx = 0;
                    _loc_2.isfull = this._checkBoxFilterFullMemberGuild.selected;
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GUILD_SEARCH, _loc_2));
                    break;
                }
                case this._btnCreateGuild:
                {
                    if (VipUtil.vipLevel < GuildListPanel.FreeCreateGuildVipLv)
                    {
                        MsgManager.showRollTipsMsg(Language.getStringByParam(71189, GuildListPanel.FreeCreateGuildVipLv));
                        return;
                    }
                    GuildCreatePanel.instance.popUp();
                    break;
                }
                case this._btnInviteList:
                {
                    if (Cache.instance.guild.inviteList.length == 0)
                    {
                        MsgManager.showRollTipsMsg("" + Language.getString(71190));
                        return;
                    }
                    GuildInviteListPanel.instance.popUp();
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function onPageChange(event:Event) : void
        {
            var _loc_2:* = this._pageSelecter.currentPage;
            var _loc_3:* = (_loc_2 - 1) * PAGE_CELL_COUNT;
            var _loc_4:* = new Object();
            _loc_4.camp = this._comboBoxFilterCamp.selectedItem == null ? (0) : (this._comboBoxFilterCamp.selectedItem.type);
            _loc_4.guildName = this._txtSearch.text;
            _loc_4.startIdx = _loc_3;
            _loc_4.isfull = this._checkBoxFilterFullMemberGuild.selected;
            Dispatcher.dispatchEvent(new DataEvent(EventName.GUILD_SEARCH, _loc_4));
            return;
        }// end function

        private function onCampSelectChange(event:Event) : void
        {
            var _loc_2:* = new Object();
            _loc_2.camp = this._comboBoxFilterCamp.selectedItem.type;
            _loc_2.guildName = this._txtSearch.text;
            _loc_2.startIdx = 0;
            _loc_2.isfull = this._checkBoxFilterFullMemberGuild.selected;
            Dispatcher.dispatchEvent(new DataEvent(EventName.GUILD_SEARCH, _loc_2));
            this._pageSelecter.currentPage = 1;
            return;
        }// end function

        private function onTextLink(event:TextEvent) : void
        {
            if (this._comboBoxFilterCamp.selectedItem.type == Cache.instance.guild.selfGuildInfo.baseInfo.camp || this._comboBoxFilterCamp.selectedItem.type == 0)
            {
                this._pageSelecter.currentPage = Math.ceil(Cache.instance.guild.selfGuildInfo.baseInfo.rank / PAGE_CELL_COUNT);
                this.onPageChange(null);
            }
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            EffectManager.glowFilterUnReg(this._btnCreateGuild);
            super.disposeImpl(param1);
            NetDispatcher.removeCmdListener(ServerCommand.GuildApplySuccess, this.updateGuide);
            this._arrow = null;
            this._circle = null;
            return;
        }// end function

        public static function get instance() : GuildListPanel
        {
            if (!_instance)
            {
                _instance = new GuildListPanel;
            }
            return _instance;
        }// end function

        public static function get hasInstance() : Boolean
        {
            return _instance != null;
        }// end function

    }
}
